﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Globalization;

namespace _4.DateParseExact
{
    class Program
    {
        static void Main(string[] args)
        {
            string input;

            input = "2.2.2011";
            PrintDateTime(input);

            input = "12.12.2011";
            PrintDateTime(input);

            input = "12.2.2011";
            PrintDateTime(input);

            input = "2.12.2011";
            PrintDateTime(input);

            input = "2.2.05";
            PrintDateTime(input);

            input = "12.12.85";
            PrintDateTime(input);

            input = "12.2.85";
            PrintDateTime(input);

            input = "2.12.85";
            PrintDateTime(input);            
        }

        private static void PrintDateTime(string input)
        {
            int day;
            int month;
            int year;

            if (TryExtractDayMonthAndYear(input, out day, out month, out year))
            {
                Console.WriteLine("day={0:00} month={1:00} year={2:0000}", day, month, year);
            }
            else
            {
                Console.WriteLine("parse failed");
            }
        }

        private static bool TryExtractDayMonthAndYear(string input, out int day, out int month, out int year)
        {
            DateTime dateTime;

            string[] formats = { "dd.MM.yyyy", "d.MM.yyyy", "d.M.yyyy", "dd.M.yyyy", "dd.MM.yy", "d.MM.yy", "d.M.yy", "dd.M.yy" };

            if (DateTime.TryParseExact(input, formats, CultureInfo.InvariantCulture, DateTimeStyles.None, out dateTime))
            {
                day = dateTime.Day;
                month = dateTime.Month;
                year = dateTime.Year;

                return true;
            }

            day = -1;
            month = -1;
            year = -1;

            return false;
        }
    }
}
